<?php

define("CCTRL", "permission");
define("CCTRLNAME", "permission");
define("TOKEN", "bb6222905ef8419183b5437779497596");

class permission extends BACKEND_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */

    function __construct() {
        parent::__construct();
        $this->load->model("backend/user_model");
        $this->smarty = new CI_Smarty3();
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
        if (!isset($_SESSION["ADP"][CCTRL]["display"]))
            $_SESSION["ADP"][CCTRL]["display"] = 0;
    }

    function index() {
        $assigns['menuactive']='menu-permission';
        $colModel = $this->user_model->colmod();
        foreach ($colModel as $col) {
            $datafields[] = array(
                'name' => $col['datafield'],
                'type' => isset($col['type']) ? $col['type'] : 'string'
            );
            $column = $col;
            $column['editable'] = isset($col['editable']) ? $col['editable'] : false;
            $column['filterable'] = isset($col['filterable']) ? $col['filterable'] : true;
            $column['sortable'] = isset($col['sortable']) ? $col['sortable'] : true;
            $column['minwidth'] = isset($col['minwidth']) ? $col['minwidth'] : (isset($col['width']) ? $col['width'] : 100);
            $columns[$col['datafield']] = $column;
            $list[] = array(
                'label' => $col['text'],
                'value' => $col['datafield'],
                'checked' => isset($col["hidden"]) ? !$col["hidden"] : true
            );
        }
        $assign = array(
            'datafields' => $datafields,
            'columns' => $columns,
            'list' => $list
        );
        $this->smarty
                ->assign($assign)
                ->display("backend/permission/01_auth_user");
    }
    function binding() {
        //$_POST=$_SESSION["tmp"];
        //$_SESSION["tmp"]=$_POST;
        $result = $this->user_model->binding();
        $data[] = array(
            'TotalRows' => $result['total_rows'],
            'Rows' => $result['rows']
        );
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($data));
    }

    function setprivilege() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if($this->privilege->aupr_permission!=777)goto result;
        $R["result"] = -998;
        $rOk = 0;
        $R["message"] = ascii_to_entities("Access is denied - Request invalid.");
        $salt = $_SESSION['auth']['user']->ause_salt;
        $aupr_user = $this->input->post('user_id');
        $aupr_privilege = $this->input->post('privilege_id');
        $aupr_permission = $this->input->post('permission');
        $secretkey = $this->input->post('secretkey');
        $signature = $this->input->post('signature');
        if ($this->privilege->aupr_permission !== '777') {
            $R["result"] = -997;
            $R["message"] = ascii_to_entities("Permission denied to access the function.");
            goto result;
        }
        //if( $signature!==md5("$aupr_privilege$secretkey$salt") ){
        //	$R["result"]=-991;
        //	$R["message"]=ascii_to_entities("Signature invalid.");
        //	goto result;
        //}
        $aParamsi = array(
            'aupr_user' => $aupr_user,
            'aupr_privilege' => $aupr_privilege,
            'aupr_permission' => $aupr_permission,
            'aupr_insert' => date('Y-m-d H:i:s'),
            'aupr_status' => 'true',
        );
        $aUpdate = array('aupr_permission', 'aupr_insert');
        //$aParamsi=objectToArray( json_decode('{"aupr_user":"10001","aupr_privilege":"10010","aupr_permission":"755","aupr_insert":"'.date('Y-m-d H:i:s').'","aupr_status":"true"}'));
        //die(json_encode($aParamsi,true));
        $rOk = $this->user_model->insert_onduplicate_update_aupr($aParamsi, $aUpdate);
        if ($rOk > 0) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Set permission success.");
        } else {
            $R["result"] = -1;
            $R["message"] = ascii_to_entities("Set permission fail.");
        }
        result:
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }

    function detail($id = "") {
        //$id=$this->input->get('id');
        $user = $this->user_model->get($id);
        if ($user === null)
            show_404();
        $privileges = $this->user_model->getprivileges($id);
        //echo $this->db->last_query();
        $time = time();
        $salt = $_SESSION['auth']['user']->ause_salt;
        $this->smarty
                ->assign(array(
                    'user' => $user,
                    'privileges' => $privileges,
                    'time' => $time,
                    'salt' => $salt
                ))
                ->display("backend/" . CCTRL . "/01_detail");
    }

}

?>